<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款方" prop="payer">
        <el-input
          v-model="queryParams.payer"
          placeholder="请输入付款方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_receivable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最后付款日">
        <el-date-picker
          v-model="daterangeLastPaymentDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:receivable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:receivable:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:receivable:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:receivable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receivableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="流水号" align="center" prop="serialNumber" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="财务类别" align="center" prop="financeCategoryName" />
      <el-table-column label="付款方" align="center" prop="payer" />
      <el-table-column label="应收金额" align="center" prop="receivableAmount" />
      <el-table-column label="付款次数" align="center" prop="paymentTimes" />
      <el-table-column label="实收金额" align="center" prop="actualReceivedAmount" />
      <el-table-column label="剩余金额" align="center" prop="actualReceivedAmount">
      <template slot-scope="scope">
        <span>{{ scope.row.receivableAmount-scope.row.actualReceivedAmount }}</span>
      </template>
      </el-table-column>
      <el-table-column label="收款方" align="center" prop="payee" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_receivable_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="最后付款日" align="center" prop="lastPaymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastPaymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 1 || scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:receivable:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.status == 1 || scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:receivable:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleAudit(scope.row,1)"
            v-hasPermi="['system:receivable:query']"
          >查看</el-button>
          <el-button v-if="scope.row.status == 1 || scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['system:receivable:audit']"
          >审核</el-button>
          <el-button v-if="(scope.row.status == 2 || scope.row.status == 4) &&
              (scope.row.receivableAmount-scope.row.actualReceivedAmount>0)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReceivables(scope.row)"
            v-hasPermi="['system:receivable:receivables']"
          >收款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应收款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流水号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入流水号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="财务类别" prop="financeCategoryId">
          <el-select v-model="form.financeCategoryId" placeholder="请选择类别">
            <el-option
              v-for="category in financeCategoryList"
              :key="category.id"
              :label="category.name"
              :value="category.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款方" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入付款方" />
        </el-form-item>
        <el-form-item label="应收金额" prop="receivableAmount">
          <el-input v-model="form.receivableAmount" placeholder="请输入应收金额" />
        </el-form-item>
        <el-form-item label="付款次数" prop="paymentTimes">
          <el-input v-model="form.paymentTimes" placeholder="请输入付款次数" />
        </el-form-item>
        <el-form-item label="收款方" prop="payee">
          <el-input v-model="form.payee" placeholder="请输入收款方" />
        </el-form-item>
        <el-form-item label="最后付款日" prop="lastPaymentDate">
          <el-date-picker clearable
            v-model="form.lastPaymentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后付款日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审核应收款对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="80px">
        <el-form-item label="流水号" prop="serialNumber">
          <el-input v-model="form1.serialNumber" placeholder="请输入流水号" disabled />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form1.name" placeholder="请输入名称"  disabled/>
        </el-form-item>
        <el-form-item label="财务类别" prop="financeCategoryId">
          <el-select v-model="form1.financeCategoryId" placeholder="请选择类别" disabled>
            <el-option
              v-for="category in financeCategoryList"
              :key="category.id"
              :label="category.name"
              :value="category.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款方" prop="payer">
          <el-input v-model="form1.payer" placeholder="请输入付款方" disabled/>
        </el-form-item>
        <el-form-item label="应收金额" prop="receivableAmount">
          <el-input v-model="form1.receivableAmount" placeholder="请输入应收金额" disabled/>
        </el-form-item>
        <el-form-item label="付款次数" prop="paymentTimes">
          <el-input v-model="form1.paymentTimes" placeholder="请输入付款次数" disabled/>
        </el-form-item>
        <el-form-item label="实收金额" prop="actualReceivedAmount">
          <el-input v-model="form1.actualReceivedAmount" placeholder="请输入实收金额" disabled/>
        </el-form-item>
        <el-form-item label="收款方" prop="payee">
          <el-input v-model="form1.payee" placeholder="请输入收款方" disabled/>
        </el-form-item>
        <el-form-item label="最后付款日" prop="lastPaymentDate">
          <el-date-picker clearable disabled
                          v-model="form1.lastPaymentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择最后付款日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="flag!=1 " type="primary" @click="updateStatus('2')">审核通过</el-button>
        <el-button v-if="flag!=1 " type="primary" @click="updateStatus('3')">审核不通过</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加收款记账对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="80px">
        <el-form-item label="流水号" prop="transactionNumber">
          <el-input v-model="form2.transactionNumber" placeholder="请输入流水号" />
        </el-form-item>
        <el-form-item label="付款机构" prop="payerInstitution">
          <el-input v-model="form2.payerInstitution" placeholder="请输入付款机构" />
        </el-form-item>
        <el-form-item label="付款方式" prop="paymentMethod">
          <el-select v-model="form2.paymentMethod" placeholder="请选择方式">
            <el-option
              v-for="category in financeCategoryList"
              :key="category.id"
              :label="category.name"
              :value="category.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款金额" prop="paymentAmount">
          <el-input v-model="form2.paymentAmount" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form2.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceivable, getReceivable, delReceivable, addReceivable, updateReceivable } from "@/api/system/receivable";
import { allFinanceCategory } from "@/api/system/financeCategory";
import { addFinanceAccounts } from "@/api/system/financeAccounts";

export default {
  name: "Receivable",
  dicts: ['sys_receivable_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      financeCategoryList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应收款表格数据
      receivableList: [],
      // 弹出层标题
      title: "",
      title1: "",
      title2: "",
      flag:null,
      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      // 备注时间范围
      daterangeLastPaymentDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: null,
        name: null,
        financeCategoryId: null,
        payer: null,
        status: null,
        lastPaymentDate: null,
      },
      // 表单参数
      form: {},
      form1: {},
      form2: {},
      // 表单校验
      rules: {
        serialNumber: [
          { required: true, message: "流水号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        payer: [
          { required: true, message: "付款方不能为空", trigger: "blur" }
        ],
        receivableAmount: [
          { required: true, message: "应收金额不能为空", trigger: "blur" }
        ],
        payee: [
          { required: true, message: "收款方不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFinanceCategoryList();
  },
  methods: {
  getFinanceCategoryList() {
    allFinanceCategory().then(response => {
      this.financeCategoryList = response;
    });
  },
    /** 查询应收款列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeLastPaymentDate && '' != this.daterangeLastPaymentDate) {
        this.queryParams.params["beginLastPaymentDate"] = this.daterangeLastPaymentDate[0];
        this.queryParams.params["endLastPaymentDate"] = this.daterangeLastPaymentDate[1];
      }
      listReceivable(this.queryParams).then(response => {
        this.receivableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNumber: null,
        name: null,
        financeCategoryId: null,
        payer: null,
        receivableAmount: null,
        paymentTimes: null,
        actualReceivedAmount: null,
        payee: null,
        status: null,
        lastPaymentDate: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset1() {
      this.form1 = {
        id: null,
        serialNumber: null,
        name: null,
        financeCategoryId: null,
        payer: null,
        receivableAmount: null,
        paymentTimes: null,
        actualReceivedAmount: null,
        payee: null,
        status: null,
        lastPaymentDate: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        id: null,
        receivableId: null,
        transactionNumber: null,
        payerInstitution: null,
        paymentMethod: null,
        paymentAmount: null,
        paymentTime: null,
        remarks: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeLastPaymentDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应收款";
    },
    /** 收款按钮操作 */
    handleReceivables(row) {
      this.reset2();
      this.form2.receivableId = row.id;
      this.open2 = true;
      this.title2 = "添加收款财务记账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReceivable(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应收款";
      });
    },
    /** 审核按钮操作 */
    handleAudit(row,flag) {
      this.flag = flag;
      this.reset1();
      const id = row.id || this.ids
      getReceivable(id).then(response => {
        this.form1 = response.data;
        this.open1 = true;
        this.title1 = "审核应收款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceivable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceivable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 收款记账提交按钮 */
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          this.form2.type = 1;
          addFinanceAccounts(this.form2).then(response => {
              this.$modal.msgSuccess("新增收款记账成功");
              this.open2 = false;
              this.getList();
            });
        }
      });
    },
    /** 审核按钮 */
    updateStatus(status) {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          this.form1.status = status;
            updateReceivable(this.form1).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open1 = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应收款编号为"' + ids + '"的数据项？').then(function() {
        return delReceivable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/receivable/export', {
        ...this.queryParams
      }, `receivable_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
